<?php
class Supplierlevel{
	public $money;
	public $order;
	public $site_id;
	public $userlist;
	public $orderdb;
	public $memberdb;
	public $member_id;
	public $error = array();
	//构造函数
	public function Supplierlevel($order_id = 0, $site_id = 0, $member_id = 0){
		$this->memberdb = D('Member');
		$this->orderdb = D('Order');
		$this->saledb = M('Sale');
		$this->money = $this->getmoney();
		$this->site_id = $site_id;
		
		$db = M('Affiliatesetting');
		$affsetting = $db -> where(array('site_id' => $this->site_id,'classname' => 'Supplierlevel')) -> find();
		$this -> affsettingarr = unserialize($affsetting['value']);
		
		$this->order = $this->orderdb -> where(array('id' => $order_id)) -> find();
		
		$this->member_id = $member_id > 0? $member_id: $this->order['member_id'];
		if(!$this->order){
			$this->error['info'] = '订单不存在！';
		}
		 
		$this->updateUserLevel();
	}
	//创建设置项表单
	public function createform(){
		
		$db = M('Affiliatesetting');
		
		$ret = array ('levelname' => array ( 0 => '股东商', 1 => '渠道商', 2 => '代理商', 3 => '分销商', ), 'levelbuy' => array ( 0 => '0', 1 => '0', 2 => '0', 3 => '0', ), 'levelaff' => array ( 0 => '0', 1 => '0', 2 => '0', 3 => '0', ), 'levelaff_' => array ( 0 => '0', 1 => '0', 2 => '0', 3 => '0', ), 'rlevel' => array ( 0 => '0', 1 => '0', 2 => '0', 3 => '0', ),'level' => array ( 0,0,0,0,0,0,0,0 ),'levelnamebase' => 'VIP会员', 'changelevel' => '恭喜您升级成为：@levelname@');
		
		
		
		$ret = $db -> where(array('site_id' => $this->site_id,'classname' => 'Supplierlevel')) -> find();
		$ret = unserialize($ret['value']);
		//F('ret',$ret);
		$html = '
<style>.Threelevel td{padding:5px;}</style><table class="Threelevel" border="1" cellspacing="0" cellpadding="5" width="100%">
  <tr>
    <th align="center" style="text-align:center; width:200px;">级别</th>
    <th align="center" style="text-align:center;">分成比例</th>
  </tr>
  
  <tr>
    <td align="center" valign="middle">会员消费提成比例</td>
    <td align="center"><input type="text" name="level[]" value="'.$ret['level'][0].'" />%</td>
  </tr>
  <tr>
    <td align="center" valign="middle">直推提成比例</td>
    <td align="center"><input type="text" name="level[]" value="'.$ret['level'][1].'" />%</td>
  </tr>
  
</table>
';

        return $html;
	}
	
	//保存设置项
	public function saveform(){
		if(isset($_POST['level'])){
			$data['classname'] = 'Supplierlevel';
			$data['value'] = serialize($_POST);
			$data['site_id'] = $this->site_id;
			$db = M('Affiliatesetting');
			$pc = (float)$_POST['rlevel'][0];
			foreach($_POST['level'] as $v){
				$pc += (float)$v;
			}
			if($pc > 100){
				$this->error['info'] = '设置错误，分成比的和大于100！';
				return false;
			}
			//dump($data);
			$ret = $db -> where(array('site_id' => $data['site_id'],'classname' => $data['classname'])) -> find();
			if($ret){
				$saveret = $db -> where(array('site_id' => $data['site_id'],'classname' => $data['classname'])) -> data(array('value' => $data['value'])) -> save();
			}else{
				$saveret = $db -> data($data) -> add();
			}
			if($saveret){
				return true; 
			}else{
				$this->error['info'] = $db -> getLastSql();
				return false;
			}
			
		}
	}
	
	
	public function updateUserLevel(){
		return false;
	}
	
	//获取等级名称
	public function getLevelName(){
		$memberInfo = $this->memberdb -> where(array('id' => $this->member_id)) -> find();
		$namelist = $this -> affsettingarr['levelname'];
		//$namelist[] = $this -> affsettingarr['levelnamebase'];
		$namelist[] = '普通会员';
		$namelist[] = 'VIP';
		//$namelist = array_reverse($namelist);
		
		return $namelist[$memberInfo['level']];
	}
	
	//获取等级名称列表
	public function getLevelNameList(){
		$memberInfo = $this->memberdb -> where(array('id' => $this->member_id)) -> find();
		$namelist = $this -> affsettingarr['levelname'];
		$namelist[] = '普通会员';
		$namelist[] = 'VIP';
		//$namelist = array_reverse($namelist);
		return $namelist;
	}
	
	//获取父子级会员数组8级
	public function getUserArr($pid=0,$arr=array(), $deep = 0){
		$re=$this->memberdb->where(array('id'=>$pid,'site_id'=>$this->site_id))->find();
		if($_GET['dump']){dump($this -> affsettingarr['VIPaff']);}
			if($deep == 0){
				$member = $this->memberdb -> where(array('id'=>$this->member_id,'site_id'=>$this->site_id)) -> find();
				
				//if($member['level'] > 0){
					$re = $member;
					$re['deep'] = $deep + 1;
				//}
			}
		//8级返回
		if($deep == 8){return $arr;}
		if(!is_array($re)){
			return $arr;
		}else{
			$re['deep'] = $deep + 1;
			//if($re['level'] > 0){
			    $arr[]=$re;
			//}
			$deep ++;
			return $this->getUserArr($re['pid'],$arr, $deep);
		}
	}
	
	//获取父子级会员数组无限级
	public function getUserArrAll($pid=0,$arr=array(), $deep = 0){
		$re=$this->memberdb->where(array('id'=>$pid,'site_id'=>$this->site_id))->find();
		if(!is_array($re)){
			return $arr;
		}else{
			if($re['level'] > 1){
				$re['deep'] = $deep + 1;
			    $arr[]=$re;
			}
			$deep ++;
			return $this->getUserArrAll($re['pid'],$arr, $deep);
		}
	}
	
	
	
	
	//获取VIP分成用户列表(需要分成的用户列表)
	public function getuserlist(){
		$ordermember = $this->memberdb -> where(array('id' => $this->order['member_id'])) -> find();
		//dump($ordermember);
		$affsettingarr = $this -> affsettingarr;
		
		$list = $this -> getUserArr($ordermember['pid']);
		/*if(!$list){
			$list[0] = $ordermember;
			$list[0]['aff'] = $affsettingarr['level'][0];
			$list[0]['deep'] = 0;
		}*/
		if(count($list) == 1){
			$member = $ordermember;
			$member['deep'] = 1;
			$list[1] = $member;
		}
		foreach($list as $k => $v){
			if($k == 0){
				$list[$k]['aff'] = $affsettingarr['level'][$k];
				
				 continue;
				
			}
			$list[$k]['aff'] = $affsettingarr['level'][$k];
			
		}
		return $list;
	}
	
	//获取总代分成总额
	public function getuserlistAll(){
		$ret = array();
		$ordermember = $this->memberdb -> where(array('id' => $this->member_id)) -> find();
		$list = $this -> getUserArrAll($ordermember['pid']);
		$affList = array_reverse($this->affsettingarr['rlevel']);//dump($affList);
		foreach($list as $k => $v){
			
			if($k == 0){
				$aff = $affList[$v['level']-2];
			}else{
				$aff = $affList[$v['level']-2] - $affList[$list[$k - 1]['level']-2];
			}
			if($aff > 0){
			    $list[$k]['aff'] = $aff;
				
				$ret[] = $list[$k];
			}
			//$list[$k]['aff'] = $affList[$v['level']-2];
		}
		return $ret;
	}
	
	
	//下单提醒（当前未付款）
	public function sendMsg(){
		
		$thismember = $this->memberdb -> where(array('id' => $this->member_id)) -> find();
		
		
		$db = M('Affiliatelog');
		$productList = $this->getmoney();
		$userList = $this->getuserlistAll();
		$userList1 = $this->getuserlist();
		$domain = C('DOMAIN_MAIN');
		$picurl = 'http://'.$domain[0].'/Public'.M('Product') -> where(array('id' => $productList[0]['product_id'])) -> getField('img_url');
		//往上发送信息
		foreach($userList as $k => $v){
			$money = 0;
			foreach($productList as $key => $val){
				$map['type'] = 1;
				$map['member_id'] = $v['id'];
				$map['order_id'] = $this->order['id'];
				$map['product_id'] = $val['product_id'];
				if($db -> where($map) -> find()){
					continue;
				}
				
				
				//写入日志，以便以后统计，注意这里不是写账户日志
				$data['site_id'] = $this->order['site_id'];
				$data['member_id'] = $v['id'];
				$data['datetime'] = time();
				$data['type'] = 1;
				$data['order_id'] = $this->order['id'];
				$data['aff_money'] = round($val['aff_money']*$v['aff']/100,2);
				$data['product_id'] = $val['product_id'];
				if($data['aff_money'] > 0){
					$money += $data['aff_money'];
					$db -> data($data) -> add();
				}
				
				
				
				
				
				
			}
			if($money > 0){
				//信息通知
				$sendWXNews[0]['title'] = '会员下单通知';
				$sendWXNews[0]['url'] = '';
				$sendWXNews[0]['picurl'] = $picurl;
				$sendWXNews[0]['description'] = '【'.$thismember['nickname'].'】下单，订单号为：'.$this->order['ordernumber'].'，会员付款后您将得到分成金额为：'.$money;
				sendWXNews($v['openid'], $sendWXNews);
			}
		}
		
		foreach($userList1 as $k => $v){
			$money = 0;
			foreach($productList as $key => $val){
				$map['type'] = 0;
				$map['member_id'] = $v['id'];
				$map['order_id'] = $this->order['id']; 
				$map['product_id'] = $val['product_id'];
				if($db -> where($map) -> find()){
					continue;
				}
				//写入日志，以便以后统计，注意这里不是写账户日志
				$data['site_id'] = $this->order['site_id'];
				$data['member_id'] = $v['id'];
				$data['datetime'] = time();
				$data['order_id'] = $this->order['id'];
				$data['aff_money'] = round($val['aff_money']*$v['aff']/100,2);
				$data['product_id'] = $val['product_id'];
				if($data['aff_money'] > 0){
					$money += $data['aff_money'];
					$db -> data($data) -> add();
				}
			}
			if($money > 0){ 
				//信息通知
				$sendWXNews[0]['title'] = '会员下单通知';
				$sendWXNews[0]['url'] = '';
				$sendWXNews[0]['picurl'] = $picurl;
				$sendWXNews[0]['description'] = '【'.$thismember['nickname'].'】下单，订单号为：'.$this->order['ordernumber'].'，会员付款后您将得到分成金额为：'.$money;
				sendWXNews($v['openid'], $sendWXNews);
			}
		}
	}
	
	//写入分成日志
	
	public function insertlogAll(){
		
		
		
		
		
		
		
		$thismember = $this->memberdb -> where(array('id' => $this->member_id)) -> find();
		$db = M('Member_account_log');
		$productList = $this->getmoney();
		$userList = $this->getuserlistAll();
		$domain = C('DOMAIN_MAIN');
		$picurl = 'http://'.$domain[0].'/Public'.M('Product') -> where(array('id' => $productList[0]['product_id'])) -> getField('img_url');
		foreach($userList as $k => $v){
			$money = 0;
			foreach($productList as $key => $val){
				
				$map['type'] = 1;
				$map['member_id'] = $v['id'];
				$map['order_id'] = $this->order['id'];
				$map['product_id'] = $val['product_id'];
				$map['attr'] = $val['attr'];
				if($db -> where($map) -> find()){
					continue;
				}
				
				
				$data['type'] = 1;
				$data['member_id'] = $v['id'];
				$data['user_id'] = 0;
				$data['pdstate'] = 3; //类型3为分成
				$data['dateline'] = time();
				
				$data['deep'] = $v['deep'];
				$data['affmoney'] = $val['aff_money'];
				
				$data['changenum'] = round($val['aff_money']*$v['aff']/100,2);
				$data['order_id'] = $this->order['id'];
				$data['order_sn'] = $this->order['ordernumber'];
				$data['product_id'] = $val['product_id'];
				$data['num'] = $val['num'];
				$data['site_id'] = $this->site_id;
				$data['attr'] = $val['attr'];
				$data['remark'] = '【'.$thismember['nickname'].'】下单，订单号为：'.$this->order['ordernumber'].'，分成金额为：'.$data['changenum'];
				$data['goodsMoney'] = $val['total'];
				if($data['changenum'] > 0){
					$money += $data['changenum'];
					
					//是否需要审核分成
					$flag = false;
					if($val['affpass'] > 0){
						$hbcount = ceil($data['changenum'] / 200);
						$hbmoney = $data['changenum'];
						for($i = 0; $i < $hbcount; $i++){
							$sendmoney = 200;
							if($hbmoney > 200){
							    $hbmoney = $hbmoney - 200;
							}else{
								$sendmoney = $hbmoney;
							}
							//dump($sendmoney);
							$result = sendBonus($v['openid'],$sendmoney,$this->site_id);
						}
						//exit;
					}
					
					$db -> data($data) -> add();
				}
			}
			if($money > 0){
				//信息通知
				$sendWXNews[0]['title'] = '订单支付通知';
				$sendWXNews[0]['url'] = '';
				$sendWXNews[0]['picurl'] = $picurl;
				$sendWXNews[0]['description'] = '【'.$thismember['nickname'].'】下单，订单号为：'.$this->order['ordernumber'].'，分成金额为：'.$money;
				//$msg = '粉丝下单，订单号为：'.$this->order['ordernumber'].'，分成金额为：'.$money;
				sendWXNews($v['openid'], $sendWXNews);
			}
		}
	}
	
	
	public function insertlog(){
		//type 0 直推，1推荐供货商，2消费，3监管
		$thismember = $this->memberdb -> where(array('id' => $this->member_id)) -> find();
		
		
		$db = M('Affiliatelog');
		$db -> where(array('order_id' => $this->order['id'])) -> delete();
		
		
		
		$db = M('Member_account_log');
		$productList = $this->getmoney();
		$userList = $this->getuserlist();
		$domain = C('DOMAIN_MAIN');
		$picurl = 'http://'.$domain[0].'/Public'.M('Product') -> where(array('id' => $productList[0]['product_id'])) -> getField('img_url');
		foreach($userList as $k => $v){
			$money = 0;
			foreach($productList as $key => $val){
				
				if($k == 0){
					$map['type'] = 2;
				}else{
					$map['type'] = 0;
				}
				$map['member_id'] = $v['id'];
				$map['order_id'] = $this->order['id'];
				$map['product_id'] = $val['product_id'];
				$map['attr'] = $val['attr'];
				if($db -> where($map) -> find()){
					continue;
				}
				
				
				$data['deep'] = $v['deep'];
				$data['affmoney'] = $val['aff_money'];
				
				if($k == 0){
					$data['type'] = 2;
				}else{
					$data['type'] = 0;
				}
				$data['member_id'] = $v['id'];
				$data['user_id'] = 0;
				$data['pdstate'] = 3; //类型3为分成
				$data['dateline'] = time();
				$data['changenum'] = round($val['aff_money']*$v['aff']/100,2);
				$data['order_id'] = $this->order['id'];
				$data['order_sn'] = $this->order['ordernumber'];
				$data['product_id'] = $val['product_id'];
				$data['num'] = $val['num'];
				$data['site_id'] = $this->site_id;
				$data['attr'] = $val['attr'];
				$data['remark'] = '【'.$thismember['nickname'].'】下单，订单号为：'.$this->order['ordernumber'].'，分成金额为：'.$data['changenum'];
				$data['goodsMoney'] = $val['total'];
				
				
				
				
				
				
				
				
				
				
				
				
				
				
				if($data['changenum'] > 0){
					$money += $data['changenum'];
					//是否需要审核分成
					$flag = false;
					if($val['affpass'] > 0){
						$hbcount = ceil($data['changenum'] / 200);
						$hbmoney = $data['changenum'];
						for($i = 0; $i < $hbcount; $i++){
							$sendmoney = 200;
							if($hbmoney > 200){
							    $hbmoney = $hbmoney - 200;
							}else{
								$sendmoney = $hbmoney;
							}
							//dump($sendmoney);
							$result = sendBonus($v['openid'],$sendmoney,$this->site_id);
						}
						//exit;
					}
					$db -> data($data) -> add();
				}
				
				
				
				
				//供货商推荐人提成
				//找查供货商对应的会员
				//$suppliermember = M('Member_supplier') -> alias('sm') -> join("left join __MEMBER__ as m on m.id = sm.member_id") -> where(array('supplier_id' => $val['supplier_id'])) -> Field('m.*') -> find();
				//已经购买的产品 
				$memberid = explode('::', $val['title']);
				
				//这个供货商的推荐人
				
				if(count($memberid) > 1 && intval($memberid[1]) > 0){
					$suppliermember = M('Member') -> where(array('id' => intval($memberid[1]))) -> Field('*') -> find();
					//查找这个会员的推荐人
					$suppliermemberparent = $this->memberdb -> where(array('id' => $suppliermember['id'])) -> find();
					
					$v = $suppliermemberparent;
					//数量，大于等于5提成1.5%,反之1%
					
					//break;
					$Member_supplierdb = M('Member_supplier');
					$saleCount = 0;
					$membersql = M('Member') -> where(array('pid' => $suppliermemberparent['id'])) -> Field('CONCAT(\'::\',id) as c') -> select();
					foreach($membersql as $vv){
						$p = M('Product') ->  where("title like '%".$vv['c']."%'") -> find();
						if($p){
							$saleCount ++;
						}
					}
					//dump($saleCount);exit;
					F('frfde', $saleCount);
					if($saleCount >= 5){
						$v = $suppliermemberparent;
						$v['aff'] = 1.5;
					}else{
						$v['aff'] = 1;
					}
					$map['type'] = 1;
					$map['member_id'] = $v['id'];
					$map['order_id'] = $this->order['id'];
					$map['product_id'] = $val['product_id'];
					$map['attr'] = $val['attr'];
					if($db -> where($map) -> find()){
						continue;
					}
					$data['deep'] = 1;
					$data['affmoney'] = $val['aff_money'];
					$data['type'] = 1;
					$data['member_id'] = $v['id'];
					$data['user_id'] = 0;
					$data['pdstate'] = 3; //类型3为分成
					$data['dateline'] = time();
					$data['changenum'] = round($val['aff_money']*$v['aff']/100,2);
					$data['order_id'] = $this->order['id'];
					$data['order_sn'] = $this->order['ordernumber'];
					$data['product_id'] = $val['product_id'];
					$data['num'] = $val['num'];
					$data['site_id'] = $this->site_id;
					$data['attr'] = $val['attr'];
					$data['remark'] = '【'.$thismember['nickname'].'】下单，订单号为：'.$this->order['ordernumber'].'，分成金额为：'.$data['changenum'];
					$data['goodsMoney'] = $val['total'];
					if($data['changenum'] > 0){
						$money += $data['changenum'];
						//是否需要审核分成
						$flag = false;
						if($val['affpass'] > 0){
							$hbcount = ceil($data['changenum'] / 200);
							$hbmoney = $data['changenum'];
							for($i = 0; $i < $hbcount; $i++){
								$sendmoney = 200;
								if($hbmoney > 200){
									$hbmoney = $hbmoney - 200;
								}else{
									$sendmoney = $hbmoney;
								}
								//dump($sendmoney);
								$result = sendBonus($v['openid'],$sendmoney,$this->site_id);
							}
							//exit;
						}
						if($map['member_id'] > 0){
							$db -> data($data) -> add();
						}
						
					}
					
				}
				//监管员提成
				//查找监管员,供货商名称中使用"【会员ID】"来识别会员，如：会员ID--供货商名称
				$supplier = M('Supplier') -> where(array('id' => $val['supplier_id'])) -> find();
				if($supplier){
					$memberid = explode('--', $val['title']);
					if(intval($memberid[1]) > 0){
						$smember = $this->memberdb -> where(array('id' => intval($memberid[1]))) -> find();
						if($smember){
							
							
							
							$v = $smember;
							$v['aff'] = 1;
							
							$map['type'] = 3;
							$map['member_id'] = $v['id'];
							$map['order_id'] = $this->order['id'];
							$map['product_id'] = $val['product_id'];
							$map['attr'] = $val['attr'];
							if($db -> where($map) -> find()){
								continue;
							}
							$data['deep'] = 1;
							$data['affmoney'] = $val['aff_money'];
							$data['type'] = 3;
							$data['member_id'] = $v['id'];
							$data['user_id'] = 0;
							$data['pdstate'] = 3; //类型3为分成
							$data['dateline'] = time();
							$data['changenum'] = round($val['aff_money']*$v['aff']/100,2);
							$data['order_id'] = $this->order['id'];
							$data['order_sn'] = $this->order['ordernumber'];
							$data['product_id'] = $val['product_id'];
							$data['num'] = $val['num'];
							$data['site_id'] = $this->site_id;
							$data['attr'] = $val['attr'];
							$data['remark'] = '【'.$thismember['nickname'].'】下单，订单号为：'.$this->order['ordernumber'].'，分成金额为：'.$data['changenum'];
							$data['goodsMoney'] = $val['total'];
							
							
							if($data['changenum'] > 0){
								$money += $data['changenum'];
								//是否需要审核分成
								$flag = false;
								if($val['affpass'] > 0){
									$hbcount = ceil($data['changenum'] / 200);
									$hbmoney = $data['changenum'];
									for($i = 0; $i < $hbcount; $i++){
										$sendmoney = 200;
										if($hbmoney > 200){
											$hbmoney = $hbmoney - 200;
										}else{
											$sendmoney = $hbmoney;
										}
										//dump($sendmoney);
										$result = sendBonus($v['openid'],$sendmoney,$this->site_id);
									}
									//exit;
								}
								$db -> data($data) -> add();
							}
							
							
						}
					}
				}
				
				
				
				
				
				
				
				
				
				
				
				
				
				
				
				
				
				
				
				
				
				
				
				
				
				
			}
			if($money > 0){
				//信息通知
				$sendWXNews[0]['title'] = '订单支付通知';
				$sendWXNews[0]['url'] = '';
				$sendWXNews[0]['picurl'] = $picurl;
				$sendWXNews[0]['description'] = '【'.$thismember['nickname'].'】下单，订单号为：'.$this->order['ordernumber'].'，分成金额为：'.$money;
				//$msg = '粉丝下单，订单号为：'.$this->order['ordernumber'].'，分成金额为：'.$money;
				sendWXNews($v['openid'], $sendWXNews);
			}
		}
		//总代分成写入
		//$this->insertlogAll();
	}
	
	//撤消一个分成
	public function loggoback(){
		
	}
	
	//获取分成总额
	public function getmoney(){
		return $this -> saledb -> where(array('order_id' => $this->order['id'])) -> Field('(aff_money*num) as aff_money,product_id,total,num,attr,affpass,supplier_id,title') -> select();
	}
	
	//返回错误信息
	public function getError(){
		return $this->error['info'];
	}
	
	
	public function getAffData(){
		$db = M('Member_account_log');
		$levelname = $this -> getLevelName();
		//type 0 直推，1推荐供货商，2消费，3监管
		
		$level = M('Member') -> where(array('id' => $this->member_id)) -> getField('level');
		//if(isset($_GET['dump'])){echo $level;dump($this->member_id);}
		$ret = array();
		//if($level > 1){ 
			
		    $data = $db -> where(array('member_id' => $this->member_id, 'site_id' => $this->site_id, 'type' => 0, 'pdstate' => 3, 'order_id' => array('gt',0))) -> Field('count(*) as orderCount, sum(changenum) as orderAffMoney, sum(goodsMoney) as orderTotal') -> select();
			//dump($db -> getLastSql());
			$ret[0]['levelname'] = '直推奖';
			//级差订单数量
			$ret[0]['orderCount'] = (int)$data[0]['orderCount'];
			//级差订单总额
			$ret[0]['orderTotal'] = (float)$data[0]['orderTotal'];
			//级差订单分成
			$ret[0]['orderAffMoney'] = (float)$data[0]['orderAffMoney'];
			
			$ret[0]['type'] = 0; 
		//}
		
		 
		//if($level > 0){
			$data = $db -> where(array('member_id' => $this->member_id, 'site_id' => $this->site_id, 'type' => 1, 'pdstate' => 3, 'order_id' => array('gt',0))) -> Field('count(*) as orderCount, sum(changenum) as orderAffMoney, sum(goodsMoney) as orderTotal') -> select();
			
			$ret[1]['levelname'] = '推荐供货商';
			//VIP订单数量
			$ret[1]['orderCount'] = (int)$data[0]['orderCount'];
			//VIP订单总额
			$ret[1]['orderTotal'] = (float)$data[0]['orderTotal'];
			//VIP订单分成
			$ret[1]['orderAffMoney'] = (float)$data[0]['orderAffMoney'];
			$ret[1]['type'] = 1;
			
			
			$data = $db -> where(array('member_id' => $this->member_id, 'site_id' => $this->site_id, 'type' => 2, 'pdstate' => 3, 'order_id' => array('gt',0))) -> Field('count(*) as orderCount, sum(changenum) as orderAffMoney, sum(goodsMoney) as orderTotal') -> select();
			
			$ret[2]['levelname'] = '自己消费';
			//VIP订单数量
			$ret[2]['orderCount'] = (int)$data[0]['orderCount'];
			//VIP订单总额
			$ret[2]['orderTotal'] = (float)$data[0]['orderTotal'];
			//VIP订单分成
			$ret[2]['orderAffMoney'] = (float)$data[0]['orderAffMoney'];
			$ret[2]['type'] = 2;
			
			
			
			$data = $db -> where(array('member_id' => $this->member_id, 'site_id' => $this->site_id, 'type' => 3, 'pdstate' => 3, 'order_id' => array('gt',0))) -> Field('count(*) as orderCount, sum(changenum) as orderAffMoney, sum(goodsMoney) as orderTotal') -> select();
			
			$ret[3]['levelname'] = '监管奖';
			//VIP订单数量
			$ret[3]['orderCount'] = (int)$data[0]['orderCount'];
			//VIP订单总额
			$ret[3]['orderTotal'] = (float)$data[0]['orderTotal'];
			//VIP订单分成
			$ret[3]['orderAffMoney'] = (float)$data[0]['orderAffMoney'];
			$ret[3]['type'] = 3;
			
			
		//}
		return $ret;
	}
	
}